﻿/*---------------------------------------------------------
 * 规则引擎
 * 
 * 用于解析业务逻辑脚本
 * 
 *--------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptNET;
using ScriptNET.Runtime;
using ScriptNET.Ast;
using ScriptNET.Processing;
using ZX.DBModule;

namespace ZX.RuleEngine.Core
{
    public  class RoleScript : ScriptNET.Script
    {
        public RoleScript()
            : base()
        {

        }

        public RoleScript(IScriptContext contenxt)
            : base(contenxt)
        {
        }
    }

    public class RoleRuntimeHost
    {
        public static void Initialize()
        {
            //加入全局
      
            //RuntimeHost.
            RuntimeHost.Initialize();
        }
        /// <summary>
        /// 用于数据检查逻辑
        /// </summary>
        /// <param name="entity">对应的实体</param>
        /// <param name="script">业务逻辑脚本</param>
        /// <returns></returns>
        public  static string CheckData(BaseEntity entity,string script)
        {
            try
            {
                Initialize();
            }catch(Exception e){}
            RuntimeHost.AddType("baseEntity", typeof(ZX.DBModule.BaseEntity));
            RuntimeHost.AddType("dtBaseEntity", typeof(ZX.DBModule.DataTableBaseEntity));
            RuntimeHost.AddType("dc", typeof(ZX.Common.Extend.DataCheck));
            RuntimeHost.AddType("bll", ZX.Common.Reflection.CreateInstance("ZX.BLL.Extend.BaseBussnies", "ZX.BLL").GetType());
            RuntimeHost.AddType("dal", typeof(ZX.DAL.DBExtend.DbSql));
            Script _script = RoleScript.Compile(script);
            _script.Context.SetItem("entity", entity);
            object rez = _script.Execute();
            return rez.ToString();
        }

        /// <summary>wb
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="script"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static string CheckData(BaseEntity entity, string script,object page)
        {
            if (page == null)
            {
                return CheckData(entity, script);
            }
            try
            {
                Initialize();
            }
            catch (Exception e) { }
            RuntimeHost.AddType("baseEntity", typeof(ZX.DBModule.BaseEntity));
            RuntimeHost.AddType("dc", typeof(ZX.Common.Extend.DataCheck));
            RuntimeHost.AddType("bll", ZX.Common.Reflection.CreateInstance("ZX.BLL.Extend.BaseBussnies", "ZX.BLL").GetType());
            RuntimeHost.AddType("dal", typeof(ZX.DAL.DBExtend.DbSql));
            Script _script = RoleScript.Compile(script);
            _script.Context.SetItem("page", page);
            _script.Context.SetItem("entity", entity);
            object rez = _script.Execute();
            return rez.ToString();
        }

        public static double? CalData(IList<DataTableBaseEntity> list, string script,string Field)
        {
            if (!script.Contains("if"))
            {
                script = "return " + script;
            }
            Script _script = RoleScript.Compile(script);
            foreach (var en in list)
            {
                _script.Context.SetItem(en.TableName, en);
            }

            object rez = _script.Execute();
            return rez as double?;
        }

        /// <summary>
        /// 运行脚本
        /// </summary>
        /// <param name="paras">
        /// 不定参数
        /// length:
        /// =1，参数为纯脚本
        /// =2，实体，脚本 或 业务逻辑，脚本
        /// =3，实体，业务逻辑对象，脚本
        /// =4, 
        /// 超过则抛出异常
        /// </param>
        /// <returns></returns>
        public static object RunCode(params object[] paras)
        {
            //----------------------------
            //定义可变参数
            //----------------------------
            switch (paras.Length)
            {
                case 0:
                    throw new  Exception("没有参数，无法进行相关操作");
                case 1:
                     Script _script = RoleScript.Compile(paras[0].ToString());
                     object rez = _script.Execute();
                     return rez;
                case 2:
                     if (paras[0] is BaseEntity)
                     {
                         return CheckData(paras[0] as BaseEntity, paras[1].ToString());
                     }
                     else
                     {
                         return "";
                     }
                default:
                     throw new Exception("未知异常");
            }
            //return "";
        }
    }
    
}
